<?php
/**
 * Created by PhpStorm.
 * User: C。
 * Date: 2019/8/13
 * Time: 9:04
 */
namespace app\home\controller;

use app\common\model\BuddhistService as BuddhistServiceModel;
use app\common\model\Company as CompanyModel;
use app\common\model\Member as MemberModel;

/**
 * BuddhistService 佛事预约
 * Class BuddhistService
 * @package app\home\controller
 */
class BuddhistService extends Base
{
    /**
     * [getIndex  个人佛事预约]
     * @param
     * @return json
     * @author [C。]
     */
    public function getIndex()
    {
        $model = new BuddhistServiceModel();
        $data = input('param.');
        $map = [];
        $this->getPageAndSize($data);
        $user = $this->user;
        try{
            if(empty($data['type'])){
                $map['b.memid'] = $user['id'];
            }
            if(!empty($data['comid'])){
                $map['b.comid'] = $data['comid'];
            }
            $field = 'b.id,c.comname,c.compic,b.type,b.addtime,b.subtime,b.isgo,b.status,b.name,b.tel,b.addr,b.remark,b.age,b.animal,b.sex';
            $count = $model->getCount($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getlist($map, $this->from, $this->size,$field);
            foreach($lists as &$v){
                $v['subtime'] = date('Y-m-d',$v['subtime']);
                $v['addtime'] = MessageTime($v['addtime']);
                $http = substr($v['compic'], 0, 4);
                if ($http != 'http') {
                    $v['compic'] = config('domain_name') . $v['compic'];
                }
                if($v['type'] == 1){
                    $v['fahui'] = '往生超度';
                }elseif($v['type'] == 2){
                    $v['fahui'] = '消灾普佛';
                }elseif($v['type'] == 3){
                    $v['fahui'] = '延生法会';
                }elseif($v['type'] == 4){
                    $v['fahui'] = '其他';
                }
            }
        }catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1,$e->getMessage());
        }
        return admin_json(1, '获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [postAdd  添加]
     * @param
     * @return json
     * @author [C。]
     */
    public function postAdd()
    {
        $param = input('param.');
        $mem = $this->user;
        $model = new BuddhistServiceModel;
        $map = [];
        $map1 = [];
        try{
            $param['subtime'] = strtotime($param['subtime']);
            $map['memid'] = $mem['id'];
            $map['subtime'] = $param['subtime'];
            $map['comid'] = $param['comid'];
            $map['type'] = $param['type'];
            $buddh = $model->getAllData($map);
            if(!empty($buddh)){
                return admin_json(-1, '您在该时间已经预约过当前寺院的该佛事');
            }

            $date = date('Y-m-d',time());
            $date1 = strtotime($date) + 24*3600;

            $map1['addtime'] = ['between',[strtotime($date),$date1]];
            $map1['memid'] = $mem['id'];
            $count = $model->getAllCount($map1);
            if($count >= 9){
                return admin_json(-1, '您当日提交次数已达上限，请明日再次提交。');
            }
            $param['memid'] = $mem['id'];
            $param['addtime'] = time();
            $model->addData($param);
            add_quality($mem['id'],25,$mem['quality'],3);
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '添加失败'.$e->getMessage());
        }
        return admin_json(1, '添加成功');
    }
    /**
     * [getGuideline  获取斋主须知]
     * @param
     * @return json
     * @author [C。]
     */
    public function getGuideline()
    {
        try{
            $guideline = get_config();
            $guideline['guideline'] = explode(';',$guideline['guideline']);
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        return admin_json(1, '获取成功',$guideline['guideline']);
    }
    /**
     * [getOne  佛事预约详情]
     * @param
     * @return json
     * @author [C。]
     */
    public function getOne()
    {
        $param = input('param.');
        $model = new BuddhistServiceModel;
        $cmodel = new CompanyModel;
        try{
            $guide = $model->findData($param['id'],'id');
            $company = $cmodel->findData($guide['comid'],'id','comname');
            $guide['comname'] = $company['comname'];
            $guide['subtime'] = date('Y-m-d',$guide['subtime']);
            $guide['addtime'] = date('Y-m-d',$guide['addtime']);
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        return admin_json(1, '获取成功',$guide);
    }
    /**
     * [getCombuddhist  寺院佛事预约]
     * @param
     * @return json
     * @author [C。]
     */
    public function getCombuddhist()
    {
        $model = new BuddhistServiceModel();
        $mmodel = new MemberModel;
        $param = input('param.');
        $map = [];
        $this->getPageAndSize($param);
        try{
            $map['b.comid'] = $param['id'];
            $field = 'b.id,b.memid,b.name,b.tel,b.subtime,b.type,b.isgo,b.remark,b.addtime,b.age,b.animal';
            $count = $model->getCount($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getlist($map, $this->from, $this->size,$field);
            foreach($lists as &$v){
                $v['subtime'] = date('Y-m-d',$v['subtime']);
                $v['addtime'] = MessageTime($v['addtime']);
                $mem = $mmodel->findData($v['memid'],'id','img');
                $http = substr($mem['img'], 0, 4);
                if ($http != 'http') {
                    $mem['img'] = config('domain_name') . $mem['img'];
                }
                $v['img'] = $mem['img'];
            }
        }catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1,$e->getMessage());
        }
        return admin_json(1, '获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [getIndex  佛事预约审核]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function postEdit()
    {
        $model = new BuddhistServiceModel;
        $param = input('param.');
        try{
            $model->editData($param);
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '修改失败'.$e->getMessage());
        }
        return admin_json(1, '修改成功');
    }
}